Imports System.Threading 'For Threads
''' -----------------------------------------------------------------------------
''' Project	 : Server
''' Class	 : Form1
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' 
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' 	[Jason Schreckengost]	6/25/2006	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtLog As System.Windows.Forms.TextBox
    Friend WithEvents txtCommand As System.Windows.Forms.TextBox
    Friend WithEvents cmdSubmit As System.Windows.Forms.Button
    Friend WithEvents lstUsers As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.txtLog = New System.Windows.Forms.TextBox
        Me.txtCommand = New System.Windows.Forms.TextBox
        Me.cmdSubmit = New System.Windows.Forms.Button
        Me.lstUsers = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'txtLog
        '
        Me.txtLog.Location = New System.Drawing.Point(8, 8)
        Me.txtLog.Multiline = True
        Me.txtLog.Name = "txtLog"
        Me.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtLog.Size = New System.Drawing.Size(392, 264)
        Me.txtLog.TabIndex = 0
        Me.txtLog.Text = ""
        '
        'txtCommand
        '
        Me.txtCommand.Location = New System.Drawing.Point(8, 280)
        Me.txtCommand.Multiline = True
        Me.txtCommand.Name = "txtCommand"
        Me.txtCommand.Size = New System.Drawing.Size(392, 24)
        Me.txtCommand.TabIndex = 1
        Me.txtCommand.Text = ""
        '
        'cmdSubmit
        '
        Me.cmdSubmit.Location = New System.Drawing.Point(408, 280)
        Me.cmdSubmit.Name = "cmdSubmit"
        Me.cmdSubmit.Size = New System.Drawing.Size(152, 24)
        Me.cmdSubmit.TabIndex = 2
        Me.cmdSubmit.Text = "Submit"
        '
        'lstUsers
        '
        Me.lstUsers.Location = New System.Drawing.Point(408, 8)
        Me.lstUsers.Name = "lstUsers"
        Me.lstUsers.Size = New System.Drawing.Size(152, 264)
        Me.lstUsers.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(568, 310)
        Me.Controls.Add(Me.lstUsers)
        Me.Controls.Add(Me.cmdSubmit)
        Me.Controls.Add(Me.txtCommand)
        Me.Controls.Add(Me.txtLog)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Main socket which will be listening
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Jason Schreckengost]	6/25/2006	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private sktServer As New clsSocket
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Main thread which will handle the listening socket
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Jason Schreckengost]	6/25/2006	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Friend thdMain As New Thread(AddressOf sktServer.Listen)
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Automatically runs when the form is loaded
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Jason Schreckengost]	6/25/2006	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Threading.Thread.CurrentThread.Name = "Main Thread"
        AddHandler sktServer.BeginListen, AddressOf BeginListenHandler
        AddHandler sktServer.ConnectionAccepted, AddressOf ConAcceptHandler
        AddHandler sktServer.DataReceived, AddressOf DataRcvdHandler
        AddHandler sktServer.DataSent, AddressOf DataWasSent
        thdMain.Start()
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Picks up the BeginListen Event from sktServer
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Jason Schreckengost]	6/25/2006	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub BeginListenHandler()
        Me.txtLog.Text = "Server was started at " + Microsoft.VisualBasic.TimeString + " on " + Microsoft.VisualBasic.DateString
        Display(vbNullString)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Picks up the ConnectionAccepted Event from sktServer
    ''' </summary>
    ''' <param name="ip">IP of the incoming connection</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Jason Schreckengost]	6/25/2006	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub ConAcceptHandler(ByVal ip As String, ByVal conID As Int32)
        Display("User Connected on Socket # " + conID.ToString + " from " + ip)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Picks up the DataReceived Event from sktServer
    ''' </summary>
    ''' <param name="user">The user element from the array Users()</param>
    ''' <param name="data">Contains the data string which was received</param>
    ''' <remarks>
    ''' See clsUser for more details about the Users() array
    ''' </remarks>
    ''' <history>
    ''' 	[Jason Schreckengost]	6/25/2006	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub DataRcvdHandler(ByVal user As clsUser, ByVal data As String)
        Dim out As String
        If Not user.Name Is Nothing Then
            out = user.Name
        Else
            out = user.IP
        End If
        Display(out + " sent a message: " + data)
    End Sub
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="user"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[Jason Schreckengost]	7/18/2006	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub Logged(ByVal user As clsUser)
        htUsers.Add(user.Username, user.ConID)
    End Sub
    Private Sub DataWasSent(ByVal username As String, ByVal data As String)
        Display(username + " sent a user: " + data)
    End Sub
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim iMax As Int32 = Users.GetUpperBound(0) - 1
        Do While iMax >= 0
            Users(iMax).Close()
            iMax -= 1
        Loop
        ReDim Users(0)
        Users = Nothing
        sktServer.Close()
        sktServer = Nothing
        thdMain.Resume()
        thdMain.Abort()
        thdMain = Nothing
    End Sub
End Class
